package com.wind.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.wind.entity.Dimensionality;
import com.wind.entity.Explain;
import com.wind.entity.TestClassify;
import com.wind.service.IDimensionalityService;
import com.wind.service.IExplainService;
import com.wind.service.ITestClassifyService;
import com.wind.util.DimToExpUtil;
import com.wind.vo.Json;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/test_classify")
public class TestClassifyController extends BaseController {

    @Autowired
    private ITestClassifyService testClassifyService;

    @Autowired
    private IDimensionalityService dimensionalityService;

    @Autowired
    private IExplainService explainService;


    @RequestMapping("/page")
    public Json page(@RequestBody String body) {
        log.info("test_classify page:{}", body);
        JSONObject json = JSON.parseObject(body);
        String name = json.getString("name");
        int current = json.getIntValue("current");
        int size = json.getIntValue("size");
        if (current == 0) current = 1;
        if (size == 0) size = 10;
        Page<TestClassify> page = testClassifyService.selectPage(new Page<>(current, size),
                new EntityWrapper<TestClassify>().like("classify_name", name)
                        .and().eq("is_active", 1)
                        .orderBy("sort", false));
        return Json.succ().data("page", page);
    }

    @RequestMapping("/add")
    public Json add(@RequestBody String body) {
        log.info("test_classify add:{}", body);
        TestClassify testClassify = JSON.parseObject(body, TestClassify.class);
        List<Long> ids = testClassify.getDimList().stream().map(Dimensionality::getId).collect(Collectors.toList());
        testClassify.setDimensionalitysId(StringUtils.join(ids, ","));
        if (!testClassifyService.insert(testClassify)) {
            return Json.fail("插入失败");
        }
        return Json.succ();
    }

    @RequestMapping("/update")
    public Json update(@RequestBody String body) {
        log.info("test_classify update:{}", body);
        TestClassify tmp = JSON.parseObject(body, TestClassify.class);
        if (!testClassifyService.updateById(tmp)) {
            return Json.fail("更新失败!");
        }
        return Json.succ();
    }

    @RequestMapping("/del")
    public Json del(@RequestBody String body) {
        log.info("test_classify del:{}", body);
        JSONObject json = JSON.parseObject(body);
        Integer id = json.getIntValue("id");
        TestClassify testClassify = testClassifyService.selectById(id);
        if (testClassify == null) {
            return Json.fail("获取分类信息失败!");
        }
        testClassify.setIsActive(0);
        if (testClassifyService.updateById(testClassify)) {
            return Json.fail("删除失败!");
        }
        return Json.succ();
    }

    /**
     * 根据ID获取维度列表
     *
     * @param id
     * @return
     */
    @GetMapping("/dimList/{id}")
    public Json getDimList(@PathVariable("id") String id) {
        List<Dimensionality> list = new ArrayList<>();
        TestClassify testClassify = testClassifyService.selectById(id);
        String[] ids = testClassify.getDimensionalitysId().split(",");
        for (String dId : ids) {
            Dimensionality dimensionality = dimensionalityService.selectById(dId);
            list.add(dimensionality);
        }
        return Json.succ().data("list", list);
    }

    /**
     * 根据ID获取分析报告列表
     *
     * @param id
     * @return
     */
    @GetMapping("/expList/{id}")
    public Json getExpList(@PathVariable("id") String id) {
        List<String> list = new ArrayList<>();
        TestClassify testClassify = testClassifyService.selectById(id);
        String[] ids = testClassify.getDimensionalitysId().split(",");
        for (String dId : ids) {
            Dimensionality dimensionality = dimensionalityService.selectById(dId);
            list.add(dimensionality.getId() + "");
        }
        List<String> dimList = DimToExpUtil.orderGroup(list, 3);
        List<Explain> exList = new ArrayList<>();
        dimList.forEach(obj -> {
            Explain explain = explainService.selectOne(new EntityWrapper<Explain>()
                    .eq("dimensionalitys_id", obj));
            exList.add(explain);
        });
        return Json.succ().data("list", list);
    }
}